package com.client.elements.normal;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.client.services.TimestampService;
import com.client.services.TimestampServiceAsync;

public class Timestampaktualisierung {

	static Label ausgabe = new Label();
	static Timer timer = new Timer() {

		TimestampServiceAsync timeservice = GWT.create(TimestampService.class);
		@Override
		public void run() {
			timeservice.getQuote(new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					ausgabe.setText("Failed");
				}

				@Override
				public void onSuccess(String result) {
					ausgabe.setText(result);
				}
			});
		}
	};

	public static Widget zufallszahlenaktualisierung(){
		FlowPanel p = new FlowPanel();
		
		ausgabe.setText("Start mit dem Button");
		ausgabe.addStyleName("zufallszahlenlabel");

		final Button btn = new Button("Start");
		btn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (btn.getText().equals("Start")) {
					timer(false);
					btn.setText("Stop");
				}else if (btn.getText().equals("Stop")) {
					timer(true);
					btn.setText("Start");
				}
			}
		});
		p.add(ausgabe);
		p.add(btn);
		return p;
	}

	private static void timer(boolean stop) {
		if (stop == false) {
			timer.scheduleRepeating(25);		
		}else {
			timer.cancel();
		}
	}
}